import axios from 'axios';
// 请求超时时间
axios.defaults.timeout = 10000;

// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
const server_url = "http://localhost:8081/"


/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params){
    let header = {}
    if( window.localStorage.getItem("token")){
         header = {
            'Authorization': window.localStorage.getItem("token") ,

        }
    }


    return new Promise((resolve, reject) =>{
        axios.get(server_url+url, {
            headers: header,
            params: params
        })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url, params) {
    let header = {}
    if( window.localStorage.getItem("token")){
        header = {
            'Authorization': window.localStorage.getItem("token") ,

        }
    }

    return new Promise((resolve, reject) => {
        axios.post(server_url+url,

            params,{
                headers: header
            })
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}